//
//  WCInterfacePerformer.m
//  WabbitCode
//
//  Created by William Towe on 2/12/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCInterfacePerformer.h"
#import "WCAppController.h"
#import "WCMacros.h"
#import "WCTextView.h"
#import "WCTextFile.h"
#import "WCRulerView.h"
#import "WCProject.h"
#import "WCLayoutManager.h"


@implementation WCInterfacePerformer
+ (WCInterfacePerformer *)sharedInstance; {
	static id _sharedInstance = nil;
	if (!_sharedInstance)
		_sharedInstance = [[[self class] alloc] init];
	return _sharedInstance;
}

- (NSScrollView *)scrollViewWithTextViewForTextFile:(WCTextFile *)textFile inProject:(WCProject *)project; {
	NSScrollView *sv = [[[NSScrollView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200.0, 200.0)] autorelease];
	[sv setBorderType:NSNoBorder];
	[sv setHasVerticalScroller:YES];
	[sv setHorizontalScroller:NO];
	[sv setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
	
	NSSize size = [sv contentSize];
	WCTextView *tv = [[[WCTextView alloc] initWithFrame:NSMakeRect(0.0, 0.0, size.width, size.height) textFile:textFile inProject:project] autorelease];
	[tv setMinSize:NSMakeSize(0.0, size.height)];
	[tv setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
	[tv setVerticallyResizable:YES];
	[tv setHorizontallyResizable:NO];
	[tv setAutoresizingMask:NSViewWidthSizable];
	
	[sv setDocumentView:tv];
	
	[[tv textContainer] replaceLayoutManager:[[[WCLayoutManager alloc] init] autorelease]];
	[[tv layoutManager] replaceTextStorage:[textFile fileTextStorage]];
	
	[tv setupDefaults];
	
	return sv;
}

- (NSMenu *)menuForProjectOutlineViewAddButton; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"WCProjectOutlineViewAddButtonMenu"] autorelease];
	
	[menu addItem:[[[NSMenuItem alloc] init] autorelease]];
	[[[menu itemArray] lastObject] setImage:[NSImage imageNamed:NSImageNameAddTemplate]];
	[[[menu itemArray] lastObject] setHidden:YES];
	
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"New File%C",@"New File with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(newFileAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"New Group",@"New Group") action:@selector(newGroupAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"New Target%C",@"New Target with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(newTargetAction:) keyEquivalent:@""];
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"New Configuration%C",@"New Configuration with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(newBuildConfigurationAction:) keyEquivalent:@""];
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"New Step%C",@"New Step with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(newBuildStepAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Existing Files%C",@"Existing Files with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(addToProjectAction:) keyEquivalent:@""];
	
	return menu;
}

- (NSMenu *)menuForProjectOutlineViewActionButton; {
	NSMenu *menu = [self contextualMenuForProjectOutlineView];
	
	[menu setTitle:@"WCProjectOutlineViewActionButtonMenu"];
	[menu removeItemAtIndex:0];
	[menu removeItemAtIndex:0];
	
	NSMenuItem *actionItem = [[[NSMenuItem alloc] init] autorelease];
	[actionItem setImage:[NSImage imageNamed:NSImageNameActionTemplate]];
	[actionItem setHidden:YES];
	[menu insertItem:actionItem atIndex:0];
	
	return menu;
}

- (NSMenu *)menuForProjectTextViewActionButton; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"WCProjectTabViewActionButtonMenu"] autorelease];
	
	[menu addItem:[[[NSMenuItem alloc] init] autorelease]];
	[[[menu itemArray] lastObject] setImage:[NSImage imageNamed:NSImageNameActionTemplate]];
	[[[menu itemArray] lastObject] setHidden:YES];
	
	[menu addItemWithTitle:NSLocalizedString(@"Shift Left",@"Shift Left") action:@selector(shiftLeftAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Shift Right",@"Shift Right") action:@selector(shiftRightAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Comment/Uncomment",@"Comment/Uncomment") action:@selector(commentUncommentAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Block Comment/Uncomment",@"Block Comment/Uncomment") action:@selector(blockCommentUncommentAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Open in Separate Editor",@"Open in Separate Editor") action:@selector(openInSeparateEditorAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Finder",@"Reveal in Finder") action:@selector(revealInFinderAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Group Tree",@"Reveal in Group Tree") action:@selector(revealInGroupTreeAction:) keyEquivalent:@""];
	/*
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"To UPPERCASE",@"To UPPERCASE") action:@selector(toUppercaseAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"To lowercase",@"To lowercase") action:@selector(toLowercaseAction:) keyEquivalent:@""];
	*/
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Jump to Definition",@"Jump to Definition") action:@selector(jumpToDefinitionAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Find Selected Text in Project",@"Find Selected Text in Project") action:@selector(findSelectedTextInProjectAction:) keyEquivalent:@""];
	[menu addItemWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Goto Line%C",@"Goto Line with ellipsis"), kUnicodeCharacterEllipsis] action:@selector(gotoLineAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Add to Bookmarks",@"Add to Bookmarks") action:@selector(addToBookmarksAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Add Breakpoint at Current Line",@"Add Breakpoint at Current Line") action:@selector(addRemoveBreakpointAtCurrentLineAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Enabled Breakpoint at Current Line",@"Enable Breakpoint at Current Line") action:@selector(enableDisableBreakpointAtCurrentLineAction:) keyEquivalent:@""];
	/*
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Show Invisible Characters",@"Show Invisible Characters") action:@selector(toggleShowInvisibleCharactersAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Show Gutter",@"Show Gutter") action:@selector(toggleShowGutterAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Show Line Numbers",@"Show Line Numbers") action:@selector(toggleShowLineNumbersAction:) keyEquivalent:@""];
	[[[menu itemArray] lastObject] setIndentationLevel:1];
	[menu addItemWithTitle:NSLocalizedString(@"Show Errors",@"Show Errors") action:@selector(toggleShowErrorsAction:) keyEquivalent:@""];
	[[[menu itemArray] lastObject] setIndentationLevel:1];
	[menu addItemWithTitle:NSLocalizedString(@"Show Warnings",@"Show Warnings") action:@selector(toggleShowWarningsAction:) keyEquivalent:@""];
	[[[menu itemArray] lastObject] setIndentationLevel:1];
	[menu addItemWithTitle:NSLocalizedString(@"Wrap Lines",@"Wrap Lines") action:@selector(toggleWrapLinesAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Indent New Lines",@"Indent New Lines") action:@selector(toggleAutomaticallyIndentNewLinesAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Insert Closing )",@"Insert Closing )") action:@selector(toggleAutomaticallyInsertClosingParenthesisAction:) keyEquivalent:@""];
	*/
	return menu;
}

- (NSMenu *)contextualMenuForProjectOutlineView; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"WCProjectOutlineViewContextualMenu"] autorelease];
	
	[menu addItemWithTitle:NSLocalizedString(@"Add",@"Add") action:NULL keyEquivalent:@""];
	[[[menu itemArray] lastObject] setSubmenu:[self menuForProjectOutlineViewAddButton]];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Open With Finder",@"Open With Finder") action:@selector(openWithFinderAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Finder",@"Reveal in Finder") action:@selector(revealInFinderAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Open in Separate Editor",@"Open in Separate Editor") action:@selector(openInSeparateEditorAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Delete",@"Delete") action:@selector(delete:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Get Info",@"Get Info") action:@selector(getInfoAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Rename",@"Rename") action:@selector(renameAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Group",@"Group") action:@selector(groupAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Ungroup",@"Ungroup") action:@selector(ungroupAction:) keyEquivalent:@""];
	
	return menu;
}
- (NSMenu *)contextualMenuForProjectFileDetailTableView; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"WCProjectFileDetailTableViewContextualMenu"] autorelease];
	
	[menu addItemWithTitle:NSLocalizedString(@"Add",@"Add") action:NULL keyEquivalent:@""];
	[[[menu itemArray] lastObject] setSubmenu:[self menuForProjectOutlineViewAddButton]];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Open With Finder",@"Open With Finder") action:@selector(openWithFinderAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Finder",@"Reveal in Finder") action:@selector(revealInFinderAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Group Tree",@"Reveal in Group Tree") action:@selector(revealInGroupTreeAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Open in Separate Editor",@"Open in Separate Editor") action:@selector(openInSeparateEditorAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Delete",@"Delete") action:@selector(delete:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Get Info",@"Get Info") action:@selector(getInfoAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Rename",@"Rename") action:@selector(renameAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Group",@"Group") action:@selector(groupAction:) keyEquivalent:@""];
	
	return menu;
}

- (NSMenu *)contextualMenuForTabViewItem:(NSTabViewItem *)tabViewItem; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"TabViewItemContextualMenu"] autorelease];
	
	[menu addItemWithTitle:NSLocalizedString(@"Reveal in Group Tree",@"Reveal in Group Tree") action:@selector(revealInGroupTreeAction:) keyEquivalent:@""];
	[[[menu itemArray] lastObject] setRepresentedObject:[tabViewItem identifier]];
	
	return menu;
}

- (NSMenu *)contextualMenuForRulerView; {
	NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"RulerViewContextualMenu"] autorelease];
	
	[menu addItemWithTitle:NSLocalizedString(@"Add Breakpoint",@"Add Breakpoint") action:@selector(_addRemoveBreakpointAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Disable Breakpoint",@"Disable Breakpoint") action:@selector(_enableDisableBreakpointAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Enable All Breakpoints in File",@"Enable All Breakpoints in File") action:@selector(_enableAllBreakpointsInFileAction:) keyEquivalent:@""];
	[menu addItemWithTitle:NSLocalizedString(@"Disable All Breakpoints in File",@"Disable All Breakpoints in File") action:@selector(_disableAllBreakpointsInFileAction:) keyEquivalent:@""];
	[menu addItem:[NSMenuItem separatorItem]];
	[menu addItemWithTitle:NSLocalizedString(@"Remove All Breakpoints in File",@"Remove All Breakpoints in File") action:@selector(_removeAllBreakpointsInFileAction:) keyEquivalent:@""];
	
	return menu;
}
@end
